package com.etpjava.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.etpjava.model.Employee;
import com.etpjava.model.Manager;
import com.etpjava.service.EmployeeService;
import com.etpjava.service.ManagementService;
import com.etpjava.service.impl.EmployeeServiceImpl;
import com.etpjava.service.impl.ManagementServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class ManagementAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6637599501007462595L;
	Log logger = LogFactory.getLog(ManagementAction.class);
	private Manager mg;
	private List<Manager> manalist;
	private Integer id;
	private ManagementService mas = new ManagementServiceImpl();
	private EmployeeService ems = new EmployeeServiceImpl();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<Manager> getManalist() {
		return manalist;
	}

	public void setManalist(List<Manager> manalist) {
		this.manalist = manalist;
	}

	public Manager getMg() {
		return mg;
	}

	public void setMg(Manager mg) {
		this.mg = mg;
	}

	public String save() {
		// logger.info("正在执行增加管理员操作===================");
		if (null != id) {
			Employee em = ems.findById(id);
			if (null != em) {
				mg.setEmployee(em);
				if (mas.maSave(mg)) {
					return SUCCESS;
				} else {
					return "fail";
				}
			} else {
				return "error";
			}

		}
		return "error";

	}

	public String beforDel() {
		// logger.info("正在执行删除前的查找操作===================");
		manalist = mas.selectAll();
		return SUCCESS;
	}

	public String del() {
		// logger.info("正在执行删除操作===================");
		if (mas.maDelete(id)) {
			return SUCCESS;
		} else {
			return "fail";
		}
	}

	public String beforUpd() {
		// logger.info("正在执行修改前的查找操作===================");
		mg = mas.selectID(id);
		return SUCCESS;
	}

	public String update() {
		// logger.info("正在执行修改操作===================");
		if (null != id) {
			Employee em = ems.findById(id);
			if (null != em) {
				mg.setEmployee(em);
			} else {
				return "error";
			}

		}
		if (mas.maChangePassword(mg)) {
			return SUCCESS;
		} else {
			return "fail";
		}
	}
}
